package com.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bean.anno.BaseConfig;
import com.bean.api.FreeApiInfo;
import com.bean.sqlite.api.*;
import com.mapper.repository.mysql.BaseConfigRepository;
import com.mapper.repository.mysql.FreeApiInfoRepository;
import com.mapper.repository.sqlite.*;
import com.mapper.sqlite.FreeApiJokesMapper;
import com.utils.BaseUtils;
import com.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Service
public class FreeApiServiceImpl implements FreeApiService {

    @Autowired
    private FreeApiInfoRepository freeApiInfoRepository;

    @Autowired
    private FreeApiJokesMapper freeApiJokesMapper;

    @Autowired
    private BaseUtils baseUtils;

    @Autowired
    private BaseConfigRepository baseConfigRepository;

    @Autowired
    private FreeApiRandomGoddessRepository freeApiRandomGoddessRepository;

    @Autowired
    private FreeApiRandomTuRepository freeApiRandomTuRepository;

    @Autowired
    private FreeApiRandomGoddessVideoRepository freeApiRandomGoddessVideoRepository;

    @Autowired
    private FreeApiRandomRiddleRepository freeApiRandomRiddleRepository;

    //分页获取 每日搞笑段子
    //Api文档：https://www.free-api.com/doc/567
    @Override
    public String addJokes(int page) {
        String url ="https://www.mxnzp.com/api/jokes/list?page=%d" +
                "&app_id=heouwsprtbpvoppt&app_secret=JaQV5hkpSZNr3SWKnNiBJkbCqMC5g5hf";
        String result = null;
        String https = String.format(url,page);
        try {
            List<FreeApiJokes> dbs = new ArrayList<FreeApiJokes>();
            result = HttpUtil.doGets(https);
            JSONObject jsonObject = JSONObject.parseObject(result);
            int code = jsonObject.getInteger("code");
            String msg = jsonObject.getString("msg");
            if(code == 1){
                JSONObject data = jsonObject.getJSONObject("data");
                JSONArray list = data.getJSONArray("list");
                Iterator iterator = list.iterator();
                while (iterator.hasNext()){
                    JSONObject joke = (JSONObject) iterator.next();
                    FreeApiJokes freeApiJokes = new FreeApiJokes();
                    freeApiJokes.setId(UUID.randomUUID().toString());
                    freeApiJokes.setContent(joke.getString("content"));
                    freeApiJokes.setCreatetime(LocalDateTime.now());
                    dbs.add(freeApiJokes);
                }

                if(dbs.size()>0){
                    freeApiJokesMapper.insertFreeApiJokes(dbs);
                }
                return "成功";
            }else{
                FreeApiInfo freeApiInfo = new FreeApiInfo();
                freeApiInfo.setId(UUID.randomUUID().toString());
                freeApiInfo.setApidesc("Api文档：https://www.free-api.com/doc/567");
                freeApiInfo.setApiurl(https);
                freeApiInfo.setApiname("每日搞笑段子");
                freeApiInfo.setStatus(msg);
                freeApiInfo.setCreatetime(LocalDateTime.now());
                freeApiInfoRepository.save(freeApiInfo);
                return "失败:"+msg;
            }


        } catch (Exception e) {
            e.printStackTrace();

            FreeApiInfo freeApiInfo = new FreeApiInfo();
            freeApiInfo.setId(UUID.randomUUID().toString());
            freeApiInfo.setApidesc("Api文档：https://www.free-api.com/doc/567");
            freeApiInfo.setApiurl(https);
            freeApiInfo.setApiname("每日搞笑段子");
            freeApiInfo.setStatus(e.getMessage());
            freeApiInfo.setCreatetime(LocalDateTime.now());
            freeApiInfoRepository.save(freeApiInfo);

            return "失败:"+e.getMessage();
        }
    }

    //调用接口获取数据   随机女神图片
    @Override
    public Map<String,Object> getRandomGoddess() {
        Map<String,Object> map = new HashMap<>();
        String api = baseUtils.getBaseConfigByName("random_goddess");
        String result = null;
        try {

            result = HttpUtil.doGets(api);
            JSONObject jsonObject = JSONObject.parseObject(result);
            int code = jsonObject.getInteger("code");
            String msg = jsonObject.getString("msg");
            if(code == 200){
                String url = jsonObject.getString("url");

                if(freeApiRandomGoddessRepository.countByContent(url)==0){
                    FreeApiRandomGoddess freeApiRandomGoddess = new FreeApiRandomGoddess();
                    freeApiRandomGoddess.setId(UUID.randomUUID().toString());
                    freeApiRandomGoddess.setContent(url);
                    freeApiRandomGoddess.setCreatetime(LocalDateTime.now());
                    freeApiRandomGoddessRepository.save(freeApiRandomGoddess);
                }
                map.put("msg","成功");
                map.put("content",url);

            }else{
                FreeApiInfo freeApiInfo = new FreeApiInfo();
                freeApiInfo.setId(UUID.randomUUID().toString());
                freeApiInfo.setApidesc("Api文档：https://www.free-api.com/doc/665");
                freeApiInfo.setApiurl(api);
                freeApiInfo.setApiname("随机女神");
                freeApiInfo.setStatus(msg);
                freeApiInfo.setCreatetime(LocalDateTime.now());
                freeApiInfoRepository.save(freeApiInfo);

                map.put("msg","成功");
                map.put("content",msg);
            }


        } catch (Exception e) {
            e.printStackTrace();

            FreeApiInfo freeApiInfo = new FreeApiInfo();
            freeApiInfo.setId(UUID.randomUUID().toString());
            freeApiInfo.setApidesc("Api文档：https://www.free-api.com/doc/665");
            freeApiInfo.setApiurl(api);
            freeApiInfo.setApiname("随机女神");
            freeApiInfo.setStatus(e.getMessage());
            freeApiInfo.setCreatetime(LocalDateTime.now());
            freeApiInfoRepository.save(freeApiInfo);

            map.put("msg","成功");
            map.put("content",e.getMessage());
        }

        return map;
    }

    //调用接口获取数据   高质量小姐姐视频
    @Override
    public Map<String,Object> getRandomGoddessVideo() {
        Map<String,Object> map = new HashMap<>();
        String api = baseUtils.getBaseConfigByName("random_goddess_video");
        String result = null;
        try {

            result = HttpUtil.doGets(api);
            JSONObject jsonObject = JSONObject.parseObject(result);
            int code = jsonObject.getInteger("code");
            String msg = jsonObject.getString("msg");
            if(code == 200){
                String url = jsonObject.getString("mp4_video");

                if(freeApiRandomGoddessVideoRepository.countByContent(url)==0){
                    FreeApiRandomGoddessVideo freeApiRandomGoddessVideo = new FreeApiRandomGoddessVideo();
                    freeApiRandomGoddessVideo.setId(UUID.randomUUID().toString());
                    freeApiRandomGoddessVideo.setContent(url);
                    freeApiRandomGoddessVideo.setCreatetime(LocalDateTime.now());

                    freeApiRandomGoddessVideoRepository.save(freeApiRandomGoddessVideo);
                }
                map.put("msg","成功");
                map.put("content",url);

            }else{
                FreeApiInfo freeApiInfo = new FreeApiInfo();
                freeApiInfo.setId(UUID.randomUUID().toString());
                freeApiInfo.setApidesc("Api文档：https://www.free-api.com/doc/665");
                freeApiInfo.setApiurl(api);
                freeApiInfo.setApiname("随机女神");
                freeApiInfo.setStatus(msg);
                freeApiInfo.setCreatetime(LocalDateTime.now());
                freeApiInfoRepository.save(freeApiInfo);

                map.put("msg","成功");
                map.put("content",msg);
            }


        } catch (Exception e) {
            e.printStackTrace();

            FreeApiInfo freeApiInfo = new FreeApiInfo();
            freeApiInfo.setId(UUID.randomUUID().toString());
            freeApiInfo.setApidesc("Api文档：https://www.free-api.com/doc/665");
            freeApiInfo.setApiurl(api);
            freeApiInfo.setApiname("随机女神");
            freeApiInfo.setStatus(e.getMessage());
            freeApiInfo.setCreatetime(LocalDateTime.now());
            freeApiInfoRepository.save(freeApiInfo);

            map.put("msg","成功");
            map.put("content",e.getMessage());
        }

        return map;
    }



    //随机获取脑筋急转弯
    //Api文档：https://api.52vmy.cn/doc/wl/s/jzw.html
    @Override
    public Map<String,Object> addRiddle() {

        Map<String,Object> map = new HashMap<>();


        String riddle = baseUtils.getBaseConfigByName("random_riddle");

        String result = null;
        try {
            result = HttpUtil.doGetsBySecond(riddle);
            JSONObject jsonObject = JSONObject.parseObject(result);
            int code = jsonObject.getInteger("code");
            String msg = jsonObject.getString("msg");
            if(code == 200){
                JSONObject data = jsonObject.getJSONObject("data");
                String question = data.getString("question");
                String answer = data.getString("answer");

                FreeApiRandomRiddle freeApiRandomRiddle = new FreeApiRandomRiddle();

                freeApiRandomRiddle.setQuestion(question);
                freeApiRandomRiddle.setAnswer(answer);

                int count = freeApiRandomRiddleRepository.countByQuestionAndAnswer(question,answer);
                if(count ==0){
                    freeApiRandomRiddle.setId(UUID.randomUUID().toString());
                    freeApiRandomRiddle.setCreatetime(LocalDateTime.now());
                    freeApiRandomRiddleRepository.save(freeApiRandomRiddle);
                }

                map.put("code",200);
                map.put("riddle",freeApiRandomRiddle);

            }else{
                FreeApiInfo freeApiInfo = new FreeApiInfo();
                freeApiInfo.setId(UUID.randomUUID().toString());
                freeApiInfo.setApidesc("Api文档：https://api.52vmy.cn/doc/wl/s/jzw.html");
                freeApiInfo.setApiurl(riddle);
                freeApiInfo.setApiname("随机脑筋急转弯");
                freeApiInfo.setStatus(msg);
                freeApiInfo.setCreatetime(LocalDateTime.now());
                freeApiInfoRepository.save(freeApiInfo);

                map.put("code",201);
                map.put("msg",msg);
            }


        } catch (Exception e) {
            e.printStackTrace();

            FreeApiInfo freeApiInfo = new FreeApiInfo();
            freeApiInfo.setId(UUID.randomUUID().toString());
            freeApiInfo.setApidesc("Api文档：https://api.52vmy.cn/doc/wl/s/jzw.html");
            freeApiInfo.setApiurl(riddle);
            freeApiInfo.setApiname("随机脑筋急转弯");
            freeApiInfo.setStatus(e.getMessage());
            freeApiInfo.setCreatetime(LocalDateTime.now());
            freeApiInfoRepository.save(freeApiInfo);

            map.put("code",201);
            map.put("msg",e.getMessage());
        }

        return  map;
    }



    @Override
    public Map<String,Object> getRandomTu(String type) {

        BaseConfig baseConfig = null;
        if(type.equals("随机帅哥")){
            baseConfig = baseConfigRepository.getByName("random_handsome");
        }else if(type.equals("随机风景")){
            baseConfig = baseConfigRepository.getByName("random_scenery");
        }else if(type.equals("随机动漫")){
            baseConfig = baseConfigRepository.getByName("random_cartoon");
        }
        String api = baseConfig.getParam();
        Map<String,Object> map = new HashMap<>();
        String result = null;
        try {

            result = HttpUtil.doGets(api);
            JSONObject jsonObject = JSONObject.parseObject(result);
            int code = jsonObject.getInteger("code");
            String msg = jsonObject.getString("msg");
            if(code == 200){
                String url = jsonObject.getString("url");

                if(freeApiRandomTuRepository.countByContent(url)==0){
                    FreeApiRandomTu freeApiRandomTu = new FreeApiRandomTu();
                    freeApiRandomTu.setId(UUID.randomUUID().toString());
                    freeApiRandomTu.setContent(url);
                    freeApiRandomTu.setCreatetime(LocalDateTime.now());
                    freeApiRandomTu.setType(type);
                    freeApiRandomTuRepository.save(freeApiRandomTu);
                }
                map.put("msg","成功");
                map.put("content",url);
                map.put("code",200);

            }else{
                FreeApiInfo freeApiInfo = new FreeApiInfo();
                freeApiInfo.setId(UUID.randomUUID().toString());
                freeApiInfo.setApidesc(baseConfig.getAnno());
                freeApiInfo.setApiurl(api);
                freeApiInfo.setApiname(type);
                freeApiInfo.setStatus(msg);
                freeApiInfo.setCreatetime(LocalDateTime.now());
                freeApiInfoRepository.save(freeApiInfo);

                map.put("msg","成功");
                map.put("content",msg);
                map.put("code",201);
            }


        } catch (Exception e) {
            e.printStackTrace();

            FreeApiInfo freeApiInfo = new FreeApiInfo();
            freeApiInfo.setId(UUID.randomUUID().toString());
            freeApiInfo.setApidesc(baseConfig.getAnno());
            freeApiInfo.setApiurl(api);
            freeApiInfo.setApiname(type);
            freeApiInfo.setStatus(e.getMessage());
            freeApiInfo.setCreatetime(LocalDateTime.now());
            freeApiInfoRepository.save(freeApiInfo);

            map.put("msg","成功");
            map.put("content",e.getMessage());
            map.put("code",201);
        }

        return map;
    }


    @Autowired
    private FreeApiQpdhRepository freeApiQpdhRepository;

    @Override
    public Map<String,Object> addQpdh() {

        BaseConfig baseConfig = baseConfigRepository.getByName("random_qpdh");

        String api = baseConfig.getParam();
        Map<String,Object> map = new HashMap<>();
        String result = null;
        try {

            result = HttpUtil.doGetsBySecond(api);
            JSONObject jsonObject = JSONObject.parseObject(result);
            int code = jsonObject.getInteger("code");
            String msg = jsonObject.getString("msg");
            if(code == 200){
                String content = jsonObject.getString("content");

                if(freeApiQpdhRepository.countByContent(content)==0){
                    FreeApiQpdh freeApiQpdh = new FreeApiQpdh();
                    freeApiQpdh.setId(UUID.randomUUID().toString());
                    freeApiQpdh.setContent(content);
                    freeApiQpdh.setCreatetime(LocalDateTime.now());
                    freeApiQpdhRepository.save(freeApiQpdh);
                }
                map.put("msg","成功");
                map.put("content",content);
                map.put("code",200);

            }else{
                FreeApiInfo freeApiInfo = new FreeApiInfo();
                freeApiInfo.setId(UUID.randomUUID().toString());
                freeApiInfo.setApidesc(baseConfig.getAnno());
                freeApiInfo.setApiurl(api);
                freeApiInfo.setApiname("奇葩对话");
                freeApiInfo.setStatus(msg);
                freeApiInfo.setCreatetime(LocalDateTime.now());
                freeApiInfoRepository.save(freeApiInfo);

                map.put("msg","成功");
                map.put("content",msg);
                map.put("code",201);
            }


        } catch (Exception e) {
            e.printStackTrace();

            FreeApiInfo freeApiInfo = new FreeApiInfo();
            freeApiInfo.setId(UUID.randomUUID().toString());
            freeApiInfo.setApidesc(baseConfig.getAnno());
            freeApiInfo.setApiurl(api);
            freeApiInfo.setApiname("奇葩对话");
            freeApiInfo.setStatus(e.getMessage());
            freeApiInfo.setCreatetime(LocalDateTime.now());
            freeApiInfoRepository.save(freeApiInfo);

            map.put("msg","成功");
            map.put("content",e.getMessage());
            map.put("code",201);
        }

        return map;
    }

}
